#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include "Person.h"
#include "Student_Func.h"
#include "Common_Func.h"

//有空节点的链表
//头节点不存放数据
pStudent_t Student_Init() {

    pStudent_t phead = (pStudent_t)malloc(sizeof(Student_t));
    if (phead == NULL)
    {
        perror("malloc Student fail");
        return phead;
    }
    memset(phead,0,sizeof(Student_t));
    phead->next = phead;
    phead->prev = phead;
    return phead;
}

//创建节点
pStudent_t Create_new_node(char *account,char *password,char *name,char sex,int height,float score){

    pStudent_t pnew=(pStudent_t)malloc(sizeof(Student_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    strcpy(pnew->account,account);
    strcpy(pnew->password,password);
    strcpy(pnew->name,name);
    pnew->sex=sex;
    pnew->height=height;
    pnew->score=score;
    pnew->status=STUDYING;
    pnew->next=NULL;
    pnew->prev=NULL;
    return pnew;
}

//从键盘创建学生节点(学生只有创建账号密码和个人信息的权限,其它都初始化为0)
pStudent_t Create_new_node_scanf(void){

    pStudent_t pnew=(pStudent_t)malloc(sizeof(Student_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    //已经将所有成员初始化为0
    memset(pnew,0,sizeof(Student_t));
    char account[50];
    char password[50];
    char name[20];
    int height;
    char sex;
    printf("请输入账号：");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("请输入密码：");
    scanf("%s",password);
    while(getchar()!='\n');
    printf("请输入姓名：");
    scanf("%s",name);
    while(getchar()!='\n');
    printf("请输入身高：");
    scanf("%d",&height);
    while(getchar()!='\n');
    printf("请输入性别: ");
    scanf("%c",&sex);
    while(getchar()!='\n');
    strcpy(pnew->account,account);
    strcpy(pnew->password,password);
    strcpy(pnew->name,name);
    pnew->sex=sex;
    pnew->height=height;
    pnew->next=NULL;
    pnew->prev=NULL;
    return pnew;
}

#if 1
//头插法
void  Student_Head_Insert(pStudent_t phead, pStudent_t node)
{   
   if(node == NULL)
   {
    perror("Head insert a node fail");
    return;
   }
   pStudent_t plink=phead;
   //判断是否首节点
   if (phead->next == phead&&phead->prev==phead)
    {
        plink->prev = node;
        plink->next = node;
        node->prev = phead;
        node->next = phead;
        printf("注册成功\n");
        printf("个人信息如下:\n");
        Print_One_Student(node);
        return;
    }
   //头插
    node->next = phead->next;
    node->prev = phead;
    phead->next->prev=node;
    phead->next = node;
    printf("注册成功\n");
    printf("个人信息如下:\n");
    Print_One_Student(node);
}

#endif


#if 1
//查找学生
void Student_Search(pStudent_t phead)
{
    if(phead->next==NULL){
        printf("链表无节点");
        return;
    }
     char name[20];
    printf("请输入要查找的姓名：");
    scanf("%s",name);
    while(getchar()!='\n');
    printf("\n");

    pStudent_t plink=phead->next;

    while(plink!=NULL)
    {
        if(strcmp(plink->name,name)==0)
        {
            printf("姓名%s,性别%c,身高%d,成绩%f",plink->name,plink->sex,plink->height,plink->score);
            return;
        }
        plink=plink->next;
    }

    if(plink==NULL)
    {
        printf("链表中找不到此数据\n");
        return;
    }

}

#endif

//删除学生信息
void Student_Random_Delet(pStudent_t phead)
{
    if(phead->next==phead)
    {
        printf("无学生信息");
        return;
    }

    pStudent_t plink=phead;
    pStudent_t ptemp=NULL;
    char account[20];
    printf("请输入要删除的账号：");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("\n");
    //plink指向需要删除节点的前一个节点
    while(strcmp(plink->next->account,account)!=0)
    {
        if(plink->next==phead)
        {
            printf("无此学生\n");
            return;
        }

        plink=plink->next;
    }
    //ptemp保存要删除的节点的指针 
    ptemp=plink->next;
    ptemp->next->prev=plink;
    plink->next=ptemp->next;
    free(ptemp);
    ptemp=NULL;
}

//修改学生信息
void Student_Modify(pStudent_t phead)
{
    char name[20],modifide_name[20];
    char sex;
    int page,choice,height,score;
    if (phead->next == phead)
    {
        printf("无学生信息");
        return;
    }

    printf("请输入要修改的学生姓名:");
    scanf("%s",name);
    while(getchar()!='\n');
 
    pStudent_t plink = phead->next;

    while (plink != phead)
    {
        if (strcmp(plink->name,name)==0)
        {
            printf("选择需要修改的信息:1.姓名  2.性别  3.身高 4.分数  5.放弃修改\n");
            scanf("%d",&choice);

            while(choice!=5)
            {
                switch(choice)
                {
                    case 1:
                        printf("请输入新的学生姓名:");
                        scanf("%s",modifide_name);
                        while(getchar()!='\n');
                        strcpy(plink->name,modifide_name);
                        break;
                    case 2:
                        printf("请输入新性别:");
                        scanf("%s",&sex);
                        plink->sex=sex;
                        break;
                    case 3:
                        printf("请输入新身高:");
                        scanf("%d",&height);
                        plink->height=height;
                        break;
                    case 4:
                        printf("请输入新成绩:");
                        scanf("%d",&score);
                        plink->score=score;
                        break;
                    case 5:
                        return;
                        break;
                    default:
                        printf("输入错误，重新输入\n");
                        break;
                }
                printf("选择需要修改的信息:1.姓名  2.性别  3.身高 4.成绩  5.放弃修改\n");
                scanf("%d",&choice);
            }
            return;
        }
        plink = plink->next;
    }

    if (plink == phead)
    {
        printf("链表中找不到此数据\n");
        return;
    }


}

//简化的学生修改信息函数
void Simplied_Student_Modify(pStudent_t plink)
{
    char name[20],modifide_name[20];
    char sex;
    int choice,height;
    printf("选择需要修改的信息:1.姓名  2.性别  3.身高   4.放弃修改\n");
    scanf("%d",&choice);

    while(choice!=4)
    {    
        switch(choice)
        {
            case 1:
                printf("请输入新的学生姓名:");
                scanf("%s",modifide_name);
                while(getchar()!='\n');
                strcpy(plink->name,modifide_name);
                break;
            case 2:
                printf("请输入新性别:");
                scanf("%s",&sex);
                while(getchar()!='\n');
                plink->sex=sex;
                break;
            case 3:
                printf("请输入新身高:");
                scanf("%d",&height);
                while(getchar()!='\n');
                plink->height=height;
                break;
            case 4:
                printf("已放弃修改\n");
                return ;
                break;
            default:
                printf("输入错误，重新输入\n");
                break;
        }
            printf("选择需要修改的信息:1.姓名  2.性别  3.身高 4.放弃修改\n");
            scanf("%d",&choice);
    }                

}


#if 1
//遍历链表
void Print_Student_List(pStudent_t phead)
{
    pStudent_t plink=phead->next;

    if(plink==phead)
    {
        perror("None Link List");
        return;
    }
    char *status[]={"在读","休学","退学"};
    while(plink!=phead)
    {
        printf("账号:%s,姓名:%s,性别:%c,身高:%d,成绩:%.2f,状态:%s\n",plink->account,plink->name,plink->sex,plink->height,plink->score,status[plink->status]);
        plink=plink->next;
    }
    printf("\n");

}

void Print_One_Student(pStudent_t plink)
{
    char *status[]={"在读","休学","退学"};
    printf("账号:%s,姓名:%s,性别:%c,身高:%d,成绩:%.2f,状态:%s\n",plink->account,plink->name,plink->sex,plink->height,plink->score,status[plink->status]);
}
#endif

#if 1
// 释放链表
void Release_Student_List(pStudent_t phead)
{
    pStudent_t plink = phead->next;
    pStudent_t ptemp = NULL;

   //不只有头结点的情况
    while (plink != phead)
    {
        ptemp = plink->next;
        free(plink);
        plink = ptemp;
    }
    free(phead);
    
    printf("链表已经全部释放\n");
}
#endif


//登录验证
pStudent_t Login_check(pStudent_t phead)
{
    if(phead->next==phead)
    {
        printf("无学生信息");
        return NULL;
    }
    pStudent_t plink=phead->next;

    char account[20];
    char password[20];
    printf("请输入账号:");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("请输入密码:");
    scanf("%s",password);
    while(getchar()!='\n');
    printf("\n");

    while(plink!=phead)
    {
        if(strcmp(plink->account,account)==0&&strcmp(plink->password,password)==0)
        {
            printf("登录成功\n");
            wait_for_any_key();
            return plink;
        }
        plink=plink->next;
    }
    printf("登录失败\n");
    wait_for_any_key();
    return NULL;

}


